#import "VerticalThreeToneSlip.h"

@implementation VerticalThreeToneSlip

-(id) init
{
    self = [super init];
    if (self) {
        _layerSlip = [CALayer layer];
        _trackLayerSlip = [CAShapeLayer layer];
        _gradientLayerSlip = [CAGradientLayer layer];
        _trackLayerSlip.fillColor = [UIColor clearColor].CGColor;
        _trackLayerSlip.strokeColor = [[UIColor blueColor] CGColor];
        _firstColor = (id)[[UIColor greenColor] CGColor];
        _secondColor = (id)[[UIColor yellowColor] CGColor];
        _thirdColor = (id)[[UIColor redColor] CGColor];
        _trackLayerSlip.lineCap = kCALineCapRound;
        _trackLayerSlip.strokeStart = 0.f;
        _trackLayerSlip.strokeEnd = 1.0f;
        _trackLayerSlip.lineWidth = 1.0f;
        _troughLineWidth = 1.0f;
        CAKeyframeAnimation *animation = [CAKeyframeAnimation
                                          animationWithKeyPath:@"transform.rotation.z"];
        animation.duration = 0.25f;
        animation.keyTimes = @[@(0), @(0.5), @(1.0)];
        animation.timingFunction = [CAMediaTimingFunction
                                    functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [self.trackLayerSlip addAnimation:animation forKey:nil];
    }
    return self;
}

-(void) updateTrackLayerSlip
{
    CGFloat hx = fabs(_trackLayerSlip.bounds.origin.x - _trackLayerSlip.bounds.size.width)/2;
    CGFloat sx = fabs(_trackLayerSlip.bounds.origin.y - _trackLayerSlip.bounds.size.height)/20;
    _trackLayerSlip.lineWidth = hx * 2 - _troughLineWidth;
    //------------------
    UIBezierPath * lineTrajectory = [UIBezierPath bezierPath];

    //起点坐标
    [lineTrajectory moveToPoint:CGPointMake(_trackLayerSlip.bounds.size.width, _trackLayerSlip.bounds.size.height)];
    //终点坐标
    [lineTrajectory addLineToPoint:CGPointMake(_trackLayerSlip.bounds.size.width, _trackLayerSlip.bounds.origin.y + sx)];

    //连线
    self.trackLayerSlip.path = lineTrajectory.CGPath;
    _trackLayerSlip.strokeStart = 0.f;
    _trackLayerSlip.strokeEnd = _value;
    CGRect valueRect;
    valueRect.origin.x = hx;
    valueRect.origin.y = sx;
    valueRect.size.width = hx * 2;
    valueRect.size.height = sx * 18;
    
    _gradientLayerSlip.frame = valueRect;
    [_gradientLayerSlip setColors:[NSArray arrayWithObjects:_firstColor,_secondColor,_secondColor,
                               _thirdColor, nil]];
    [_gradientLayerSlip setLocations:@[@0.6,@0.65,@0.9,@0.95]];
    [_gradientLayerSlip setStartPoint:CGPointMake(0.5, 1)];
    [_gradientLayerSlip setEndPoint:CGPointMake(0.5, 0)];
    [_layerSlip addSublayer:_gradientLayerSlip];
    //设置渐变层也是圆角
    //_gradientLayerSlip.cornerRadius = hx + _troughLineWidth;
    _gradientLayerSlip.cornerRadius = hx;
    [_layerSlip setMask:_trackLayerSlip];
}

-(void) setFirstColor:(UIColor *)firstColor
{
    if(firstColor != _firstColor)
    {
        _firstColor = (id)firstColor.CGColor;
        [self updateTrackLayerSlip];
    }
}

-(void) setSecondColor:(UIColor *)secondColor
{
    if(secondColor != _secondColor)
    {
        _secondColor = (id)secondColor.CGColor;
        [self updateTrackLayerSlip];
    }
}

-(void) setThirdColor:(UIColor *)thirdColor
{
    if(thirdColor != _thirdColor)
    {
        _thirdColor = (id)thirdColor.CGColor;
        [self updateTrackLayerSlip];
    }
}

-(void) setTroughLineWidth:(CGFloat)troughLineWidth
{
    if(troughLineWidth != _troughLineWidth)
    {
        _troughLineWidth = troughLineWidth;

        [self updateTrackLayerSlip];
    }
}

-(void) setValue:(CGFloat) value
{
    if(value != _value)
    {
        _value = value;
        [self updateTrackLayerSlip];
    }
}
-(void)updateWithBounds:(CGRect)bounds
{
    //设置图层位置大小
    self.trackLayerSlip.bounds = bounds;
    self.trackLayerSlip.position = CGPointMake(CGRectGetWidth(bounds)/2.0, CGRectGetHeight(bounds)/2.0);
    [self updateTrackLayerSlip];
}

@end